package com.anyikang.volunteer.sos.volunteer.app.adapter;
import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.anyikang.volunteer.sos.volunteer.R;
import com.anyikang.volunteer.sos.volunteer.app.MyApplication;
import com.anyikang.volunteer.sos.volunteer.app.MyConstant;
import com.anyikang.volunteer.sos.volunteer.app.bean.SosListItem;
import com.anyikang.volunteer.sos.volunteer.utils.CommonUtil;

import java.util.List;

/**
 * xw.gao
 * 报警记录列表，listview型适配器
 */
public class SosRecordAdapter extends BaseAdapter {
    private Context mContext;
    private List<SosListItem> mRecordList;
    private String mDeviceIMEI;

    public SosRecordAdapter(List<SosListItem> recordList, Context context,String deviceIMEI) {
        this.mContext = context;
        this.mRecordList = recordList;
        this.mDeviceIMEI = deviceIMEI;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        SosListItem item = mRecordList.get(position);
        ViewHolder holder = null;

        if (convertView == null) {
            // 复用乱序问题
            convertView = LayoutInflater.from(mContext).inflate(R.layout.sos_record_item, null);
            holder = new ViewHolder();
            holder.imvPerson = (ImageView) convertView.findViewById(R.id.imvPerson);
            holder.tvName = (TextView) convertView.findViewById(R.id.tvName);
            holder.tvAddress = (TextView) convertView.findViewById(R.id.tvAddress);
            holder.tvSosTime = (TextView) convertView.findViewById(R.id.tvSosTime);
            holder.tvSosState = (TextView) convertView.findViewById(R.id.tvSosState);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        String name = item.getName();
        String address = item.getAddress();
        long sosTime = item.getSosTime();
        String strSosTime = String.valueOf(sosTime+"");
        String strSosTime2 = CommonUtil.getDateTimeYmdHms(Long.parseLong(strSosTime));
        int state = item.getState();

        //姓名
        if(!TextUtils.isEmpty(name))
        {
            holder.tvName.setText(name);
        }

        //求救地址
        if(!TextUtils.isEmpty(address)) {
            holder.tvAddress.setText(address);
        }

        //求救时间
        if(!TextUtils.isEmpty(strSosTime2)) {
            holder.tvSosTime.setText(strSosTime2);
        }

        switch(state)
        {
            case 1:
                holder.tvSosState.setText("需要救助");
                break;
            case 2:
                holder.tvSosState.setText("救助中");
                break;
            case 3:
                holder.tvSosState.setText("已完成");
                break;
        }
      String imageUrl = MyConstant.IMAGE_ADDRESS + item.getImageUrl();
      com.nostra13.universalimageloader.core.ImageLoader.getInstance().displayImage(imageUrl, holder.imvPerson, MyApplication.optionsForPerson);

        return convertView;
    }

    @Override
    public int getCount() {
        return mRecordList.size();
    }

    @Override
    public Object getItem(int position) {
        return mRecordList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    class ViewHolder {
        ImageView imvPerson;
        TextView tvName;
        TextView tvAddress;
        TextView tvSosTime;
        TextView tvSosState;
    }

}